package org.budo.dubbo.protocol.http.view.render.budo;

import org.budo.support.slf4j.Slf4j;
import org.budo.validation.exception.AbstractBudoValidationException;
import org.budo.validation.exception.BudoLengthValidationException;
import org.budo.validation.exception.BudoMaxValidationException;
import org.budo.validation.exception.BudoMinValidationException;
import org.budo.validation.exception.BudoNotBlankValidationException;
import org.budo.validation.exception.BudoNotEmptyValidationException;
import org.budo.validation.exception.BudoNotNullValidationException;
import org.budo.validation.exception.BudoPatternValidationException;
import org.budo.validation.exception.BudoSizeValidationException;
import org.slf4j.Logger;

/**
 * @author lmw
 */
public class ValidationExceptionRender {
    private static final Logger log = Slf4j.getLogger();

    public ApiError buildApiError(AbstractBudoValidationException exception) {
        if (exception instanceof BudoNotNullValidationException //
                || exception instanceof BudoNotBlankValidationException //
                || exception instanceof BudoNotEmptyValidationException) {
            return ApiError.MISS_REQUIRED_PARAMETER.withDescription(exception.getMessage());
        }

        if (exception instanceof BudoLengthValidationException) {
            return ApiError.PARAMETER_LENGTH_EXCEED.withDescription(exception.getMessage());
        }

        if (exception instanceof BudoSizeValidationException) {
            return ApiError.PARAMETER_SIZE_EXCEED.withDescription(exception.getMessage());
        }

        if (exception instanceof BudoMinValidationException) {
            return ApiError.PARAMETER_TOO_SMALL.withDescription(exception.getMessage());
        }

        if (exception instanceof BudoMaxValidationException) {
            return ApiError.PARAMETER_TOO_BIG.withDescription(exception.getMessage());
        }

        if (exception instanceof BudoPatternValidationException) {
            return ApiError.PARAMETER_PATTERN_MISMATCH.withDescription(exception.getMessage());
        }

        log.error("#46 renderException other validation fail, exception=" + exception, exception);
        return ApiError.ILLEGAL_PARAMETER;
    }
}